<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="x-ua-compatible" content="ie=7" />

	<title>Project Template</title>

	<script>
		try{
			window.resizeTo(0,0);
			window.moveTo(-300,0);
		}catch(e){
		}
	</script>

	<hta:application
		id="oHTA"
		innerBorder="no"
		maximizeButton="no"
		scroll="no"
		icon="regedit.exe"
		selection="no"
		border="dialog"
	/>

	<style>
	body{
		background-color:#F0F0F0;
	}
	div{
		font-family:微软雅黑;
		user-select:none;
		position:absolute;
		border-width:1px;
		border-style:solid;
		border-color:#F0F0F0;
	}
	.tip{
		overflow:hidden;
	}
	.item_list{
		border-color:#DDDDDD;
		overflow-y:auto;
	}
	.item{
		position:static;
		min-width:90%;
		max-width:100%;
		background-color:#FFFFFF;
		border-color:#FFFFFF;
		overflow:hidden;
		text-overflow: ellipsis;
		white-space:nowrap;
	}
	.btn{
		background-color:#E1E1E1;
		border-color:#CDCDCD;
		text-align:center;
	}
	.btn:hover{
		border-color:#0078D7;
		background-color:#E5F1FB;
	}
	.btn:active{
		border-color:#005499;
		background-color:#CCE4F7;
	}
	</style>

	<script>
	var dpi_ = screen.deviceYDPI;
	var items_ = [];
	var show_count_ = 0;
	var selected_item_ = null;

	var fso_ = new ActiveXObject("Scripting.FileSystemObject");

	var lang_zh_ = {};
	lang_zh_.yes = "是";
	lang_zh_.no = "否";
	lang_zh_.select = "请选择要创建的项目:";
	lang_zh_.copying = "复制项目文件 ...";
	lang_zh_.overwrite = "项目已存在是否覆盖?";

	var lang_en_ = {};
	lang_en_.yes = "Yes";
	lang_en_.no = "No";
	lang_en_.select = "Select Project:";
	lang_en_.copying = "Copy Files ...";
	lang_en_.overwrite = "Project exists, overwrite?";

	var lang_ = lang_en_;
	if(navigator.browserLanguage && navigator.browserLanguage.indexOf('zh')>=0){
		lang_ = lang_zh_;
	}

	function getTemplateFolder(){
		var thisDir = oHTA.commandLine;
		var index = thisDir.lastIndexOf('\\');
		var findex = 0;
		if(thisDir.charAt(0) == '"')findex = 1;
		return thisDir.substring(findex, index) + "\\template";
	}

	function pathGetName(p){
		var name = "" + p;
		var off = name.lastIndexOf("\\");
		return name.substring(off + 1, name.length);
	}

	function getTemplates(){
		var dir = getTemplateFolder();
		if(!fso_.FolderExists(dir)){
			return [];
		}
		var dir = fso_.GetFolder(dir);
		var files = dir.SubFolders;
		var fc = new Enumerator(files);
		var ret = [];
		for(;!fc.atEnd(); fc.moveNext()){
			ret.push(pathGetName(fc.item()));
		}
		return ret;
	}

	function isExists(src, dst){
		var folder = fso_.GetFolder(src);
		var folders = folder.SubFolders;
		var fc = new Enumerator(folders);
		for(;!fc.atEnd(); fc.moveNext()){
			if(fso_.FolderExists(dst + "\\" + pathGetName(fc.item())))return true;
		}
		var files = folder.Files;
		var ffc = new Enumerator(files);
		for(;!ffc.atEnd(); ffc.moveNext()){			
			if(fso_.FileExists(dst + "\\" + pathGetName(ffc.item())))return true;
		}
		return false;
	}

	function onItemHover(){
		if(selected_item_)return;
		event.srcElement.style.backgroundColor = "#E5F1FB";
		event.srcElement.style.borderColor = "#E5F1FB";
	}

	function onItemLostHover(){
		if(selected_item_)return;
		event.srcElement.style.backgroundColor = "#FFFFFF";
		event.srcElement.style.borderColor = "#FFFFFF";
	}

	function doCopy(){
		var selected = selected_item_.innerText;
		var src = getTemplateFolder() + "\\" + selected;
		fso_.CopyFolder(src, ".", true);
		setTimeout(function(){
			window.close();
		}, 300);
	}

	function showTip(tipmsg, showopt){
		tip.innerText = tipmsg;
		btn_no.style.visibility = showopt?"visible":"hidden";
		btn_yes.style.visibility = showopt?"visible":"hidden";
	}

	function onBtnYes(){
		showTip(lang_.copying, false);
		doCopy();
	}

	function onBtnNo(){
		if(selected_item_){
			selected_item_.style.backgroundColor = "";
			selected_item_.style.borderColor = "";
			selected_item_ = null;
		}
		showTip(lang_.select, false);
	}

	function onItemClick(){
		if(selected_item_)return;
		selected_item_ = event.srcElement;
		selected_item_.style.backgroundColor = "#CCE8FF";
		selected_item_.style.borderColor = "#99D1FF";
		var src = getTemplateFolder() + "\\" + selected_item_.innerText;
		if(isExists(src, ".")){
			showTip(lang_.overwrite, true);
			return;
		}
		doCopy();
	}

	function showItems(){
		var s = "";
		var items = items_;
		for(var i=0;i<items.length;i++){
			var name = items[i];
			s += ("<div class=\"item\" onclick=\"onItemClick()\" onmouseover=\"onItemHover()\" onmouseout=\"onItemLostHover()\">");
			s += name;
			s += "</div>";
		}
		item_list.innerHTML = s;
	}

	function resizeWindow(){
		var w = 360 * dpi_ / 96;
		var h = (32*show_count_ + 100) * dpi_ / 96;
		var x = (screen.width - w) / 2;
		var y = (screen.height - h) / 2;
		window.resizeTo(w, h);
		window.moveTo(x, y);
	}

	function moveElement(el, x, y, w, h, borderW){
		el.style.left = x + "px";
		el.style.top = y + "px";
		el.style.width = w + "px";
		el.style.height = h + "px";
		el.style.lineHeight = h + "px";
		el.style.borderWidth = borderW + "px";
	}

	function resizeElements(){
		var dpi = screen.deviceYDPI;
		var bodyW = document.documentElement.clientWidth;
		var bodyH = document.documentElement.clientHeight;		
		var borderW = Math.round(0.9 * dpi_ / 96);
		var btnW = Math.round(75 * dpi_ / 96 - (borderW*2));
		var btnH = Math.round(28 * dpi_ / 96 - (borderW*2));
		var margin = Math.round(8 * dpi_ / 96);
		var tipW = bodyW - btnW * 2 - margin * 3;		
		var itemH = Math.round((bodyH - btnH - margin * 3 - (borderW*2)-4) / show_count_);
		var listH = itemH * show_count_;

		document.documentElement.style.fontSize = Math.round(14*dpi/96) + 'px';

		moveElement(tip, margin+borderW, margin+borderW, tipW, btnH, borderW);
		moveElement(btn_yes, bodyW - btnW*2 - margin*2 - borderW, margin+borderW, btnW, btnH, borderW);
		moveElement(btn_no, bodyW - btnW - margin - borderW, margin+borderW, btnW, btnH, borderW);
		moveElement(item_list, margin, bodyH-margin-listH - borderW, bodyW-margin*2, listH, borderW);

		var nodes = item_list.childNodes;
		for(var i=0;i<nodes.length;i++){
			var h = itemH - (borderW*2);
			nodes[i].style.height = h + "px";
			nodes[i].style.lineHeight = h + "px";
			nodes[i].style.paddingLeft = margin + "px";
			nodes[i].style.borderWidth = borderW + "px";
		}
	}

	window.onload = function(){
		items_ = getTemplates();
		show_count_ = items_.length;
		if(show_count_ > 15)show_count_ = 15;
		showItems();
		showTip(lang_.select, false);
		btn_yes.innerText = lang_.yes;
		btn_no.innerText = lang_.no;
		resizeWindow();
	}
	window.onresize = resizeElements;
	</script>
</head>
<body>
	<div class="tip" id="tip"></div>
	<div class="btn" id="btn_yes" onclick="onBtnYes()"></div>
	<div class="btn" id="btn_no"  onclick="onBtnNo()"></div>
	<div class="item_list" id="item_list" ></div>
</body>
</html>

